import warnings
warnings.warn("qturboframe.public_ui.ufc_wpf.button模块已被废弃，替代模块为qturboframe.public_ui.ucf_wpf.button", DeprecationWarning)

from qturboframe.lib.utils.by import By
from qturboframe.lib.report.log import logger
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.win_control.button import Button as WinButton
from qturboframe.public_ui.ufc_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ufc_wpf.utili.common import Common
from qturboframe.lib.ranorex.win_control.control import Control


class Button(BaseControl):
    """
    ucf wpf的按钮类

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - button: 当前的button对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: Button({By.TEXT: 'Click'})
     - 例2: Button({By.AUTOMATIONID:'automationid'})
     - 例3: Button(control)
     - 例4: Button({By.TEXT: 'Click'}, father=client)
     - 例5: Button({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: Button(control, father=client)
    """

    def __init__(self, element_para, father=None, timeout=10000):
        if isinstance(element_para, dict) and By.TEXT in element_para:
            if hasattr(father,"_control") and isinstance(father._control, Control):
                father = father._control
            text = element_para.get(By.TEXT)
            text = Common.update_special_character(text)
            xpath = ".//button[@text~'{}']".format(text, text)
            control = father.find_first_visible_element(By.XPATH, xpath, timeout=timeout)
            if control:
                super().__init__(control, father, timeout)
            else:
                raise CsClientError("没有找到此控件:{}".format(element_para))
        else:
            super().__init__(element_para, father, timeout)

        self.button = WinButton(self._control.control)

    def click(self):
        """
        按钮点击

        :Example:
         - 例: button.click()
        """
        button_text = self.button.get_text()
        if button_text:
            logger.info('点击按钮：' + button_text)
        else:
            logger.info('点击按钮！')
        self.button.ensure_visible()
        self.button.click()

    def get_text(self):
        """
        获取按钮上的文字

        :Example:
         - 例: button.get_text()
        """
        return self.button.get_text()

    def is_enabled(self):
        """
        获取按钮是否可以点击

        :Example:
         - 例: button.is_enabled()
        """
        return self.button.is_enable()